home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 408_01 / pccharst.c < prev    next >
Text File  |  1993-08-16  |  19KB  |  518 lines

  1. /*
  2.     SNEWS 1.91
  3.  
  4.     pccharst - routines to handle multiple character sets for IBM PCs
  5.  
  6.  
  7.     Copyright (C) 1993  Daniel Fandrich
  8.                         <dan@fch.wimsey.bc.ca> or CompuServe 72365,306
  9.  
  10.     This program is free software; you can redistribute it and/or modify
  11.     it under the terms of the GNU General Public License, version 1, as
  12.     published by the Free Software Foundation.
  13.  
  14.     This program is distributed in the hope that it will be useful,
  15.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.     GNU General Public License for more details.
  18.  
  19.     See the file COPYING, which contains a copy of the GNU General
  20.     Public License.
  21.  
  22.  
  23.     Source is formatted with a tab size of 4.
  24.  
  25.  */
  26.  
  27.  
  28. #include "defs.h"
  29. #include "pccharst.h"
  30. #include <dos.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33.  
  34.  
  35. #define DEFAULT_CP 437    /* mapping to use if active CP isn't supported */
  36.  
  37. #define NOCH '?'        /* character to show for those invalid in current code page */
  38.  
  39.  
  40. enum code_pages code_page_table;
  41.  
  42. /* these names must match enum code_pages */
  43. struct {int code_page_num; enum code_pages cptable;} code_page_list[] = {
  44.     437, CP437,        /* US */
  45.     850, CP850,        /* Multilingual/Latin I */
  46.  
  47. #if 0    /* these aren't in the table yet */
  48.     852, CP852,        /* Slavic/Latin II */
  49.     857, CP857,        /* Turkish/Latin V */
  50.     860, CP860,        /* Portugal */
  51.     861, CP861,        /* Iceland */
  52.     862, CP862,        /* Hebrew */
  53.     863, CP863,        /* Canadian - French */
  54.     865, CP865,        /* Norway/Denmark */
  55.     866, CP866,        /* Cyrillic */
  56.     869, CP869,        /* Greek */
  57. #endif
  58.     0, CP_NOT_SUPP    /* end of list */
  59. };
  60.  
  61. /* these names must match enum char_sets */
  62. char *char_set_names[/*enum char_sets*/] = {
  63.     "X-IBM437",        /* IBM code page 437     code page 437 is best */
  64.     "ISO-8859-1",    /* Latin 1                code page 850 is best */
  65.     "ISO-8859-2",   /* Latin 2                code page 852 is best */
  66.     "ISO-8859-3",    /* Latin 3                code page 850 is best? */
  67.     "ISO-8859-4",    /* Latin 4                code page 850 is best? */
  68.     "ISO-8859-9",    /* Latin 5                code page 857 is best */
  69. #if 0    /* these aren't in the mapping table yet */
  70.     "ISO-8859-5",    /* Latin/Cyrillic        code page 866 is best */
  71.     "ISO-8859-6",    /* Latin/Arabic */
  72.     "ISO-8859-7",    /* Latin/Greek          code page 869 is best */
  73.     "ISO-8859-8",    /* Latin/Hebrew */
  74.     "ISO-8859-10",    /* Latin 6                code page 852 is best? */
  75. #endif
  76.     "US-ASCII",        /* (must be last in table) equivalent to ISO-8859-1 */
  77.     NULL
  78. };
  79.  
  80.  
  81. /* cptable[code_pages][char_sets][character code] */
  82. char cptable[2 /*8*/][6/*12*/][0x80] = {
  83.  
  84. { /* code page 437 mappings */
  85.  
  86. /* Map from code page 437 to code page 437 */
  87. {
  88. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
  89. 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
  90. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
  91. 0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
  92. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
  93. 0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
  94. 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,
  95. 0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
  96. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,
  97. 0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
  98. 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,
  99. 0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
  100. 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,
  101. 0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
  102. 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
  103. 0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
  104. },
  105.  
  106. /* Map from ISO 8859-1 to code page 437 */
  107. {
  108. /* 128 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  109. /* 136 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  110. /* 144 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  111. /* 152 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  112. /* 160 */    0xFF,0xAD,0x9B,0x9C,0x0F,0x9D,0x7C,0x15,
  113. /* 168 */    '"', '@', 0xA6,0xAE,0xAA,0xC4,NOCH,'-',
  114. /* 176 */    0xF8,0xF1,0xFD,'3', 0x27,0xE6,0x14,0xFA,
  115. /* 184 */    ',', '1', 0xA7,0xAF,0xAC,0xAB,NOCH,0xA8,
  116. /* 192 */    'A', 'A', 'A', 'A', 0x8E,0x8F,0x92,0x80,
  117. /* 200 */   'E', 0x90,'E', 'E', 'I', 'I', 'I', 'I',
  118. /* 208 */    'D', 0xA5,'O', 'O', 'O', 'O', 0x99,'X',
  119. /* 216 */    0xE9,'U', 'U', 'U', 0x9A,'Y', NOCH,0xE1,
  120. /* 224 */    0x85,0xA0,0x83,'a', 0x84,0x86,0x91,0x87,
  121. /* 232 */    0x8A,0x82,0x88,0x89,0x8D,0xA1,0x8C,0x8B,
  122. /* 240 */    NOCH,0xA4,0x95,0xA2,0x93,'o', 0x94,0xF6,
  123. /* 248 */    0xED,0x97,0xA3,0x96,0x81,'y', NOCH,0x98
  124. },
  125.  
  126. /* Map from ISO 8859-2 to code page 437 */
  127. {
  128. /* 128 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  129. /* 136 */   NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  130. /* 144 */   NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  131. /* 152 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  132. /* 160 */    0xFF,'A', NOCH,'L', 0x0F,'L', 'S', 0x15,
  133. /* 168 */    '"', 'S', 'S', 'T', 'Z', 0xC4,'Z', 'Z',
  134. /* 176 */    0xF8,'a', NOCH,'l', 0x27,'l', 's', NOCH,
  135. /* 184 */    ',', 's', 's', 't', 'z', '"', 'z', 'z',
  136. /* 192 */    'R', 'A', 'A', 'A', 0x8E,'L', 'C', 0x80,
  137. /* 200 */   'C', 0x90,'E', 'E', 'E', 'I', 'I', 'D',
  138. /* 208 */    'D', 'N', 'N', 'O', 'O', 'O', 0x99,'X',
  139. /* 216 */    'R', 'U', 'U', 'U', 0x9A,'Y', 'T', 0xE1,
  140. /* 224 */    'r', 0xA0,0x83,'a', 0x84,'l', 'c', 0x87,
  141. /* 232 */    'c', 0x82,'e', 0x89,'e', 0xA1,0x8C,'d',
  142. /* 240 */    0xEB,'n', 'n', 0xA2,0x93,'o', 0x94,0xF6,
  143. /* 248 */    'r', 'u', 0xA3,'u', 0x81,'y', 't', 0xF9
  144. },
  145.  
  146. /* Map from ISO 8859-3 to code page 437 */
  147. {
  148. /* 128 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  149. /* 136 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  150. /* 144 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  151. /* 152 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  152. /* 160 */    0xFF,'H', NOCH,0x9C,0x0F,NOCH,'H', 0x15,
  153. /* 168 */    '"', 'I', 'S', 'G', 'J', 0xC4,NOCH,'Z',
  154. /* 176 */    0xF8,'h', 0xFD,'3', 0x27,0xE6,'h', 0xFA,
  155. /* 184 */    ',', 'i', 'd', 'g', 'j', 0xAB,NOCH,'z',
  156. /* 192 */    'A', 'A', 'A', NOCH,0x8E,'C', 'C', 0x80,
  157. /* 200 */   'E', 0x90,'E', 'E', 'I', 'I', 'I', 'I',
  158. /* 208 */    NOCH,0xA5,'O', 'O', 'O', 'G', 0x99,'X',
  159. /* 216 */    'G', 'U', 'U', 'U', 0x9A,'U', 'S', 0xE1,
  160. /* 224 */    0x85,0xA0,0x83,NOCH,0x84,'c', 'c', 0x87,
  161. /* 232 */    0x8A,0x82,0x88,0x89,0x8D,0xA1,0x8C,0x8B,
  162. /* 240 */    NOCH,0xA4,0x95,0xA2,0x93,'g', 0x94,0xF6,
  163. /* 248 */    'g', 0x97,0xA3,0x96,0x81,'u', 's', 0xF9
  164. },
  165.  
  166. /* WHAT IS GREENLANDIC K (162) AND LAPPISH ENG (189,191)? */
  167. /* Map from ISO 8859-4 to code page 437 */
  168. {
  169. /* 128 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  170. /* 136 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  171. /* 144 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  172. /* 152 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  173. /* 160 */    0xFF,'A', 'k', 'R', 0x0F,'I', 'L', 0x15,
  174. /* 168 */    '"', 'S', 'E', 'G', 'T', 0xC4,'Z', '-',
  175. /* 176 */    0xF8,'a', NOCH,'r', 0x27,'i', 'l', NOCH,
  176. /* 184 */    ',', 's', 'e', 'g', 't', NOCH,'z', NOCH,
  177. /* 192 */    'A', 'A', 'A', 'A', 0x8E,0x8F,0x92,'I',
  178. /* 200 */   'C', 0x90,'E', 'E', 'E', 'I', 'I', 'I',
  179. /* 208 */    'D', 'N', 'O', 'K', 'O', 'O', 0x99,'X',
  180. /* 216 */    0xE9,'U', 'U', 'U', 0x9A,'U', 'U', 0xE1,
  181. /* 224 */    'a', 0xA0,0x83,'a', 0x84,0x86,0x91,'i',
  182. /* 232 */    'c', 0x82,'e', 0x89,'e', 0xA1,0x8C,'i',
  183. /* 240 */    0xEB,'n', 'o', 'k', 0x93,'o', 0x94,0xF6,
  184. /* 248 */    0xED,'u', 0xA3,0x96,0x81,'u', 'u', 0xF9
  185. },
  186.  
  187. /* Map from ISO 8859-9 to code page 437 */
  188. {
  189. /* 128 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  190. /* 136 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  191. /* 144 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  192. /* 152 */    NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,NOCH,
  193. /* 160 */    0xFF,0xAD,0x9B,0x9C,0x0F,0x9D,0x7C,0x15,
  194. /* 168 */    '"', '@', 0xA6,0xAE,0xAA,0xC4,NOCH,'-',
  195. /* 176 */    0xF8,0xF1,0xFD,'3', 0x27,0xE6,0x14,0xFA,
  196. /* 184 */    ',', '1', 0xA7,0xAF,0xAC,0xAB,NOCH,0xA8,
  197. /* 192 */    'A', 'A', 'A', 'A', 0x8E,0x8F,0x92,0x80,
  198. /* 200 */   'E', 0x90,'E', 'E', 'I', 'I', 'I', 'I',
  199. /* 208 */    'G', 0xA5,'O', 'O', 'O', 'O', 0x99,'X',
  200. /* 216 */    0xE9,'U', 'U', 'U', 0x9A,'I', 'S', 0xE1,
  201. /* 224 */    0x85,0xA0,0x83,'a', 0x84,0x86,0x91,0x87,
  202. /* 232 */    0x8A,0x82,0x88,0x89,0x8D,0xA1,0x8C,0x8B,
  203. /* 240 */    'g', 0xA4,0x95,0xA2,0x93,'o', 0x94,0xF6,
  204. /* 248 */    0xED,0x97,0xA3,0x96,0x81,'i', 's', 0x98
  205. }
  206. },
  207.  
  208.  
  209. { /* code page 850 mappings */
  210.  
  211. /* Map from code page 437 to code page 850 */
  212. {
  213. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
  214. 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
  215. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
  216. 0x98,0x99,0x9A,0xBD,0x9C,0xBE,NOCH,0x9F,
  217. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
  218. 0xA8,0xDA,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
  219. 0xB0,0xB1,0xB2,0xB3,0xB4,0